<template>
  <div class="pagination">
    <!-- 当前页码为1时不能操作 -->
    <button
      :disabled="currentPage === 1"
      @click="setCurrentPage(currentPage - 1)"
    >
      <!-- &lt; -->
      <i class="el-icon-arrow-left"></i>
    </button>
    <!-- start要大于1才显示 -->
    <button v-if="startEnd.start > 1" @click="setCurrentPage(1)">1</button>
    <!-- start要大于2才显示 -->
    <button v-if="startEnd.start > 2" disabled>···</button>

    <!-- 连续页码 [start, end] -->
    <button
      v-for="(item, index) in startEnd.end"
      :key="index"
      v-if="item >= startEnd.start"
      :class="{ active: item === currentPage }"
      @click="setCurrentPage(item)"
    >
      {{ item }}
    </button>

    <!--
        v-for的优先级高于v-if: 先执行v-for的遍历, 每遍历一个再进行v-if的判断 面试题
        -->
    <!-- 只有在end小于totalPages-1 -->
    <button v-if="startEnd.end < totalPages - 1" disabled>···</button>
    <!-- 只有在end小于totalPages -->
    <button
      v-if="startEnd.end < totalPages"
      @click="setCurrentPage(totalPages)"
    >
      {{ totalPages }}
    </button>
    <!-- 当当前页码为totalPages时不能操作 -->
    <button
      :disabled="currentPage === totalPages"
      @click="setCurrentPage(currentPage + 1)"
    >
      <i class="el-icon-arrow-right"></i>
    </button>

    <!-- <button style="margin-left: 30px">{{totalPages}}</button> -->
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["total", "pageSize", "continuePages", "currentPageWrap"],
  data() {
    return {
      currentPage: this.currentPageWrap,
    };
  },
  computed: {
    // 总页数
    totalPages() {
      const { total, pageSize } = this;
      return Math.ceil(total / pageSize);
    },
    // 连续页码数
    startEnd() {
      let start;
      let end;
      start = this.currentPage - Math.floor(this.continuePages / 2);
      start < 1 ? (start = 1) : "";
      end = this.continuePages + start - 1;
      if (end > this.totalPages) {
        end = this.totalPages;
        start = end - this.continuePages + 1;
        start < 1 ? (start = 1) : "";
      }
      return { start, end };
    },
  },
  methods: {
    setCurrentPage(index) {
      if (index > this.totalPages) return;
      if (index < 1) return;
      if (this.currentPage === index) return;
      this.currentPage = index;
      this.$emit("changeCurrentPage", index);
    },
  },
  watch: {
    currentPageWrap(newVal) {
      this.currentPage = newVal;
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  display: flex;
  justify-content: flex-end;
  // display: inline-block;
  // margin-right: 10px;
  button {
    margin: 0 5px;
    background-color: #ffffff;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 32px;
    height: 32px;
    line-height: 32px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 1PX solid rgba(0, 0, 0, 0.12);;
    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }
    &.active {
      cursor: not-allowed;
      background-color: #000000;
      color: #fff;
    }
  }
}
</style>
